#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1005;
int n;
struct node{
    int t;
    int s;
    bool operator < (const node &b)const{
        return s<b.s;
    }
}task[maxn];

int main(){
    cin>>n;
    int x,y;
    for(int i=1;i<=n;i++){
        cin>>x>>y;
        task[i].t=x;
        task[i].s=y;
    }
    sort(task+1,task+n+1);
    int ans = 0;
    int s=task[n].s;
    
    for(int i=n;i>0;i--){
        int ss = min(task[i].s,s);
        s=ss-task[i].t;
    }
    if(s<0)	cout<<-1<<endl;
    else	cout<<s<<endl;
}
